package com.masu.knows.portal.mapper;

import com.masu.knows.portal.model.Permission;
import com.masu.knows.portal.model.Role;
import com.masu.knows.portal.model.User;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.masu.knows.portal.vo.UserVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
* <p>
    *  Mapper 接口
    * </p>
*
* @author 沈传尚
* @since 2021-12-07
*/
@Repository
public interface UserMapper extends BaseMapper<User> {

    // 根据用户id查询所有权限的方法
    @Select("SELECT p.id, p.name" +
            " FROM user u" +
            " LEFT JOIN user_role ur ON u.id=ur.user_id" +
            " LEFT JOIN role r ON r.id=ur.role_id" +
            " LEFT JOIN role_permission rp" +
            " ON rp.role_id=r.id" +
            " LEFT JOIN permission p" +
            " ON p.id=rp.permission_id" +
            " WHERE u.id=#{id}")
    List<Permission> getPermissionsByUserId(Integer id);


    // 根据用户名查询用户对象
    @Select("select * from user where username=#{username}")
    User findUserByUsername(String username);


    // 根据用户名查询userVo信息
    @Select("select id, username ,nickname from user where username=#{username}")
    UserVo findUserVOByUsername(String username);


    //根据用户id查询出用户的所有角色
    List<Role> findAllRolesById(@Param("userId") Integer userId);




}
